"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const geoserver_js_1 = require("./util/geoserver.js");
const about_js_1 = __importDefault(require("./about.js"));
class WorkspaceClient {
    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
    }
    async getAll() {
        const response = await fetch(this.url + 'workspaces.json', {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async get(name) {
        const response = await fetch(this.url + 'workspaces/' + name + '.json', {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const grc = new about_js_1.default(this.url, this.auth);
            if (await grc.exists()) {
                return;
            }
            else {
                const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
                throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
        return response.json();
    }
    async create(name) {
        const body = {
            workspace: {
                name
            }
        };
        const response = await fetch(this.url + 'workspaces', {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(body)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            switch (response.status) {
                case 409:
                    throw new geoserver_js_1.GeoServerResponseError('Unable to add workspace as it already exists', geoServerResponse);
                default:
                    throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
        const wsObject = await this.get(name);
        return wsObject.workspace.name;
    }
    async delete(name, recurse) {
        const response = await fetch(this.url + 'workspaces/' + name + '?recurse=' + recurse, {
            credentials: 'include',
            method: 'DELETE',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            switch (response.status) {
                case 400:
                    throw new geoserver_js_1.GeoServerResponseError('Workspace or related Namespace is not empty (and recurse not true)', geoServerResponse);
                case 404:
                    throw new geoserver_js_1.GeoServerResponseError('Workspace doesn\'t exist', geoServerResponse);
                default:
                    throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
    }
}
exports.default = WorkspaceClient;
